import {
  reqGetDepartmentListForIdAndName,
  reqGetDepartmentList,
  reqBatchAddDepartment,
  reqEidtDepartment,
  reqBatchDelDepartment,
  reqCheckDepartmentNameIsRepeat,
} from '@/views/Custom/AcademyMng/api/departmentApi';
import {
  reqGetMajorList,
  reqBatchAddMajor,
  reqEidtMajor,
  reqBatchDelMajor,
  reqGetMajorListByDepartmentId,
  reqCheckMajorNameIsRepeat,
  reqGetMajorListForIdAndName,
} from '@/views/Custom/AcademyMng/api/majorApi';
import {
  reqGetClassroomList,
  reqQueryDepAndMaJorAndClassroom,
  reqBatchAddClassroom,
  reqEditClassroom,
  reqBatchDelClassroom,
  reqCheckClassroomNameIsRepeat,
} from '@/views/Custom/AcademyMng/api/classroomApi';

const state = {
  departmentIPage: {}, // 院系分页信息对象
  departmentList: [],
  departmentListForIdAndName: [], // 院系编号与名称集合
  majorIPage: {}, // 专业分页信息对象
  majorList: [],
  majorsByDepId: [], // 根据院系编号查询出来的专业集合
  majorListForIdAndName: [], // 专业编号与名称集合
  classroomIPage: [], // 班级分页信息
  classroomList: [],
  depAndMajorTree: [], // 院系专业树
};

const mutations = {
  GETDEPARTMENTLIST(state, result) {
    state.departmentIPage = result;
  },
  // 获取院系的编号与名称列表
  GETDEPARTMENTLISTFORIDANDNAME(state, result) {
    state.departmentListForIdAndName = result;
  },
  // 获取专业的编号与名称列表
  GETMAJORLISTFORIDANDNAME(state, result) {
    state.majorListForIdAndName = result;
  },

  // 查询专业列表
  GETMAJORLIST(state, result) {
    state.majorIPage = result;
  },

  // 根据院系编号查询专业集合
  GETMAJORLISTBYDEPARTMENTID(state, result) {
    state.majorsByDepId = result;
  },

  // 根据专业编号查询班级信息
  GETCLASSROOMLIST(state, result) {
    state.classroomIPage = result;
  },
  // 获取专业院系树
  GETDEPANDMAJORMENU(state, result) {
    state.depAndMajorTree = result;
  },
};

const actions = {
  // 获取院系的编号与名称列表
  async getDepartmentListForIdAndName({ commit }) {
    let reqsult = await reqGetDepartmentListForIdAndName();
    commit(
      'GETDEPARTMENTLISTFORIDANDNAME',
      reqsult.data.departmentListForIdAndName
    );
  },
  // 查询院系
  async getDepartmentList({ commit }, params) {
    let result = await reqGetDepartmentList(params);
    commit('GETDEPARTMENTLIST', result.data.departmentIPage);
  },
  // 批量添加院系
  async batchAddDepartmentList({ commit }, params) {
    let result = await reqBatchAddDepartment(params);
    return result;
  },
  // 编辑院系
  async editDepartment({ commit }, params) {
    let result = await reqEidtDepartment(params);
    return result;
  },
  // 删除院系
  async batchDelDepartment({ commit }, params) {
    let result = await reqBatchDelDepartment(params);
    return result;
  },

  // 根据院系编号获取专业信息
  async getMajorListByDepartmentId({ commit }, params) {
    let result = await reqGetMajorListByDepartmentId(params);
    commit(
      'GETMAJORLISTBYDEPARTMENTID',
      result.data ? result.data.majorsByDepId : []
    );
    return result;
  },
  // 获取专业的编号与名称列表
  async getMajorListForIdAndName({ commit }) {
    let reqsult = await reqGetMajorListForIdAndName();
    commit('GETMAJORLISTFORIDANDNAME', reqsult.data.majorListForIdAndName);
  },
  // 查询专业
  async getMajorList({ commit }, params) {
    let result = await reqGetMajorList(params);
    commit('GETMAJORLIST', result.data.majorIPage);
    return result;
  },
  // 批量添加专业
  async batchAddMajorList({ commit }, params) {
    let result = await reqBatchAddMajor(params);
    return result;
  },
  // 编辑专业
  async editMajor({ commit }, params) {
    let result = await reqEidtMajor(params);
    return result;
  },
  // 删除专业
  async batchDelMajor({ commit }, params) {
    let result = await reqBatchDelMajor(params);
    return result;
  },
  // 检查专业名称是否重复
  async checkIsRepeatForMajorName({ commit }, params) {
    let result = await reqCheckMajorNameIsRepeat(params);
    return result;
  },
  // 检查院系名称是否重复
  async checkIsRepeatForDepartmentName({ commit }, params) {
    let result = await reqCheckDepartmentNameIsRepeat(params);
    return result;
  },
  // 检查班级名称是否重复
  async checkIsRepeatForClassroomName({ commit }, params) {
    let result = await reqCheckClassroomNameIsRepeat(params);
    return result;
  },

  // 查询班级信息
  async getClassroomList({ commit }, params) {
    let result = await reqGetClassroomList(params);
    commit('GETCLASSROOMLIST', result.data.classroomIPage);
    return result;
  },
  // 获取院系专业树
  async getDepAndMajorMenu({ commit }) {
    let result = await reqQueryDepAndMaJorAndClassroom();
    commit('GETDEPANDMAJORMENU', result.data.depAndMajorMenu);
  },
  // 班级新增
  async batchAddClassroom({ commit }, params) {
    let result = await reqBatchAddClassroom(params);
    return result;
  },
  // 班级编辑
  async editClassroom({ commit }, params) {
    let result = await reqEditClassroom(params);
    return result;
  },
  // 班级删除
  async delClassroom({ commit }, params) {
    let result = await reqBatchDelClassroom(params);
    return result;
  },
};
const getters = {
  departmentIPage(state) {
    return state.departmentIPage || [];
  },
  departmentList(state) {
    return state.departmentIPage ? state.departmentIPage.records || [] : [];
  },
  majorIPage(state) {
    return state.majorIPage || [];
  },
  majorList(state) {
    return state.majorIPage ? state.majorIPage.records || [] : [];
  },
  departmentListForIdAndName(state) {
    return state.departmentListForIdAndName || [];
  },
  majorsByDepId(state) {
    return state.majorsByDepId || [];
  },
  classroomIPage(state) {
    return state.classroomIPage || [];
  },
  classroomList(state) {
    return state.classroomIPage ? state.classroomIPage.records || [] : [];
  },
  depAndMajorTree(state) {
    return state.depAndMajorTree || [];
  },
  majorListForIdAndName(state) {
    return state.majorListForIdAndName || [];
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
